import random
import time
import jwt
import numpy as np
from django.shortcuts import render

# Create your views here.
from django import http

# 找到轮转的数据
from rest_framework.views import APIView

from index.models import Shop
from product.models import User,Cart,OrderAddress
from rest_framework.response import Response
from .models import OrderCart

from houduan.utils.huyi_sms.sms3 import send_sms_code


class SubmitTrade(APIView):
    """用户名登录视图"""
    def post(self,request,*args,**kwargs):

        token = request.data.get('token', '').strip()
        user = User.objects.filter(token = token).first()

        buyName = request.data.get('buyName', '').strip()
        buyPhone = request.data.get('buyPhone', '').strip()
        deliveryDate = request.data.get('deliveryDate', '').strip()
        remarks = request.data.get('remarks', '').strip()
        userAddressId = int(request.data.get('userAddressId', '0'))

        cart_list = request.data.get('cartList', [])

        for cart_item in cart_list:
            blessing = cart_item.get('blessing', '')
            count = int(cart_item.get('count', '0'))
            screateTime = cart_item.get('createTime', '')
            shop_id = int(cart_item.get('goodsId', '0'))
            imageUrl = str(cart_item.get('imageUrl', '')),
            name = cart_item.get('name', '')
            price = cart_item.get('price', '')
            supdateTime = cart_item.get('updateTime', '')
            data = getNewName()
            OrderCart.objects.create(buyName=buyName,buyPhone=buyPhone,
                                    blessing=blessing,count=count,screateTime=screateTime,
                                    supdateTime=supdateTime,imageUrl=imageUrl[0],name=name,price=price,
                                     deliveryDate=deliveryDate,remarks=remarks,status=1,
                                     addrID_id=userAddressId,shop_id=shop_id,user_id=user.id,
                                     data=data)


        return Response({ "data":data,"code": 200,"message":"成功"})

def getNewName():
    new_name = time.strftime('%Y%m%d%H%M%S', time.localtime())
    ranlist = np.random.randint(0, 10, 5)
    for i in ranlist:
        new_name += str(i)
    # 返回字符串
    return new_name